﻿using MediatR;
using Microsoft.EntityFrameworkCore;
using W.Common.Enum;
using W.Warehouse.Api.Application.Command.AllocationCommand;
using W.Warehouse.Api.Application.Command.CheckCommand;
using W.Warehouse.Domain;
using W.Warehouse.Domain.AllocationProduct;
using W.Warehouse.Repository.Impl;
using W.Warehouse.Repository.Interface;

namespace W.Warehouse.Api.Application.CommandHandlder.CheckCommandHandler
{
    public class CheckCreateCommandHandler: IRequestHandler<CheckCreateCommand, int>
    {
        private readonly ICheckRepository _icheckRepository;
        private readonly IStatisticsRepository _istatisticsRepository;
        public CheckCreateCommandHandler(ICheckRepository icheckRepository, IStatisticsRepository istatisticsRepository)
        {
            _icheckRepository = icheckRepository;
            _istatisticsRepository = istatisticsRepository;
        }

        public async Task<int> Handle(CheckCreateCommand request, CancellationToken cancellationToken)
        {
            using (var tran = _icheckRepository.DB.Database.BeginTransaction(System.Data.IsolationLevel.Serializable))
            {
                try
                {
                    #region 盘点信息表添加数据
                    Check check=new Check();
                    check.CheckId = request.CheckId;
                    check.CheckNo = request.CheckNo;
                    check.AssociateNumber = request.AssociateNumber;
                    check.CheckMakeHe = request.CheckMakeHe;
                    check.AssociateNumber = request.AssociateNumber;
                    check.CheckDate = request.CheckDate;
                    check.CheckNotes = request.CheckNotes;
                    check.ProductId = request.ProductId;
                    check.AuditStatus = AuditStatus.等待审核;
                    check.CheckType = "产品盘点";
                    check.office_id = request.office_id;
                    check.user_id = request.user_id;
                    check.ExamineReason = request.ExamineReason;
                    check.ExaminePeople = request.ExaminePeople;
                    check.ExamineTime = request.ExamineTime;
                    check.CreateName = "张三";
                    check.CreateTime = DateTime.Now;
                    check.IsDelete = false;
                    await _icheckRepository.AddAsync(check);
                    #endregion

                    #region 盘点产品信息表添加数据
                    foreach (var item in request.StatisticsList)
                    {
                        Statistics statistics =new Statistics();
                        statistics.StatisticsId = item.StatisticsId;
                        statistics.ProductName = item.ProductName;
                        statistics.ProductNo = item.ProductNo;
                        statistics.ProductSpec = item.ProductSpec;
                        statistics.StatisticsBatch = item.StatisticsBatch;
                        statistics.UnitId = item.UnitId;
                        statistics.BinLocationName = item.BinLocationName;
                        statistics.office_id = item.office_id;
                        statistics.user_id = item.user_id;
                        statistics.StatisticsSum = item.StatisticsSum;
                        statistics.CheckId = check.CheckId;
                        statistics.CreateName = "张三";
                        statistics.CreateTime = DateTime.Now;
                        statistics.IsDelete = false;
                        await _istatisticsRepository.AddAsync(statistics);

                    }
                    #endregion
                    var code = await _icheckRepository.DB.SaveChangesAsync();
                    tran.Commit();
                    return code;
                }
                catch (Exception ex)
                {
                    tran.Rollback();
                    throw;
                }
            }
        }
    }
}
